package com.lzy.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lzy.dao.key.IMFolderDao;
import com.lzy.dao.key.KeyIMDao;
import com.lzy.entity.IMFolder;
import com.lzy.entity.KeyIM;
import com.lzy.entity.dto.TreeNode;
import com.lzy.exception.WrapedException;
import com.lzy.util.CommUtil;
import com.lzy.util.Page;

@Service
@Transactional
public class KeyIMServiceImpl implements KeyIMService{
	
	private IMFolderDao imFolderDao ;
	
	private KeyIMDao keyIMDao ;
	
	@Autowired
	public void setKeyIMDao(KeyIMDao keyIMDao) {
		this.keyIMDao = keyIMDao;
	}
	
	@Autowired
	public void setImFolderDao(IMFolderDao imFolderDao) {
		this.imFolderDao = imFolderDao;
	}
	
	@Transactional(readOnly=true)
	public List<TreeNode> treeItemsQuery() throws Exception {
		List<TreeNode> treeList = new ArrayList<TreeNode>();
		List<IMFolder> roots = this.imFolderDao.queryByParentId(-1);
		TreeNode node ;
		for (Iterator<IMFolder> iterator = roots.iterator(); iterator.hasNext();) {
			IMFolder imFolder = iterator.next();
			node = new TreeNode();
			node.setId(imFolder.getTypeID());
			node.setText(imFolder.getTypeName());
			treeList.add(node);
			getChildren(node);
		}
		return treeList;
	}
	
	private void getChildren(TreeNode node) {
		List<IMFolder> imFolders = this.imFolderDao.queryByParentId(node.getId());
		if(imFolders==null || imFolders.size()==0){
//			node.setLeaf(true);
		}else{
			for (Iterator<IMFolder> iterator = imFolders.iterator(); iterator.hasNext();) {
				IMFolder imFolder = iterator.next();
				TreeNode subNode = new TreeNode();
				subNode.setId(imFolder.getTypeID());
				subNode.setText(imFolder.getTypeName());
				node.getChildren().add(subNode);
				getChildren(subNode);
			}
		}
	}

	public void addFolder(Map<String, Object> params) {
		IMFolder folder = new IMFolder();
		folder.setParentId( CommUtil.Param2Int(params, "parentId") );
		folder.setTypeName( CommUtil.Param2String(params, "typeName") );
		this.imFolderDao.add(folder);
	}

	@Transactional
	public void delFolder(int id) {
		this.imFolderDao.del(id);
	}

	public void updateFolder(Map<String, Object> params) {
		int typeId = CommUtil.Param2Int(params, "typeId");
		IMFolder folder = this.imFolderDao.query(typeId);
		folder.setTypeName( CommUtil.Param2String(params, "typeName") );
		this.imFolderDao.update(folder);
	}

	public void addKeyIm(Map<String, Object> params) throws WrapedException {
		String id = CommUtil.Param2String(params, "id");
		if(this.keyIMDao.isExist(id)){
			throw new WrapedException( "用户账号不能重复!!!");
		}
		
		KeyIM keyIM = new KeyIM();
		keyIM.setCreateTime(new Date());
		keyIM.setId(id);
		keyIM.setCatalogId(CommUtil.Param2Int(params, "catalogId"));
		keyIM.setEnable(CommUtil.Param2Int(params, "enable"));
		keyIM.setIsSendFile(CommUtil.Param2Int(params, "isSendFile"));
		keyIM.setIsVideo(CommUtil.Param2Int(params, "isVideo"));
		keyIM.setIsVoice(CommUtil.Param2Int(params, "isVoice"));
		keyIM.setRemark(CommUtil.Param2String(params, "remark"));
		
		this.keyIMDao.add(keyIM);
	}

	public void updateKeyIm(Map<String, Object> params) {
		KeyIM keyIM = this.keyIMDao.queryById(CommUtil.Param2String(params, "id"));
		keyIM.setEnable(CommUtil.Param2Int(params, "enable"));
		keyIM.setIsSendFile(CommUtil.Param2Int(params, "isSendFile"));
		keyIM.setIsVideo(CommUtil.Param2Int(params, "isVideo"));
		keyIM.setIsVoice(CommUtil.Param2Int(params, "isVoice"));
		keyIM.setRemark(CommUtil.Param2String(params, "remark"));
		this.keyIMDao.update(keyIM);
	}

	public void delKeyIm(Map<String, Object> params) {
		String ids = CommUtil.Param2String(params, "id");
		String[] strings = ids.split(",");
		for (int i = 0; i < strings.length; i++) {
			this.keyIMDao.del(strings[i]);
		}
	}

	public KeyIM queryKeyImById(String id) {
		return this.keyIMDao.queryById(id);
	}
	
	public Page queryKeyIm(Map<String, Object> params) {
		int typeId = CommUtil.Param2Int(params, "typeId");
		return this.keyIMDao.queryByCatalogId(typeId);
	}
	
	
}